package com.ld.collegetask;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.beans.MemberWalletBean;
import com.ld.collegetask.databinding.ActivityMemberWalletBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberWalletActivity extends BaseActivity {

    private ActivityMemberWalletBinding binding;

    private Integer userId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_wallet);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_wallet);

        SharePreference sp = new SharePreference(getBaseContext());
        userId = sp.getUserId();

        setTitleView(this, binding.ilTitle, true, true, false, false);
        setLoadingView(this,binding.ilLoading);
        setTitle("我的钱包");
        findView();

        // 设置下拉进度的背景颜色，默认就是白色的
        binding.swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);

        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                member_wallet();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loading_go();
    }

    @Override
    public void loading() {
        member_wallet();
    }

    public void findView() {
        binding.memberWalletChargeApply.setOnClickListener(this);
        binding.memberWalletCashApply.setOnClickListener(this);
        binding.memberWalletCash.setOnClickListener(this);
        binding.memberWalletCharge.setOnClickListener(this);
        binding.memberWalletTurnover.setOnClickListener(this);
        binding.memberWalletDiscount.setOnClickListener(this);
    }

    private void member_wallet() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_wallet(userId);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    MemberWalletBean memberWallet = gson.fromJson(base.getJson(), MemberWalletBean.class);
                    binding.setMemberWalletBean(memberWallet);
                    loading_end();
                } else {
                    loading_error();
                }
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 加载完数据设置为不刷新状态，将下拉进度收起来
                        binding.swipeRefreshLayout.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 加载完数据设置为不刷新状态，将下拉进度收起来
                        binding.swipeRefreshLayout.setRefreshing(false);
                    }
                });
                loading_error();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.member_wallet_charge_apply:                   //跳转到充值
                startActivity(new Intent(mActivity, MemberWalletChargeApplyActivity.class));
                break;
            case R.id.member_wallet_cash_apply:                   //跳转到提现
                startActivity(new Intent(mActivity, MemberWalletCashApplyActivity.class));
                break;
            case R.id.member_wallet_turnover:                        //跳转到收支记录
                startActivity(new Intent(MemberWalletActivity.this, MemberWalletTurnoverListActivity.class));
                break;
            case R.id.member_wallet_charge:                      //跳转到充值记录
                startActivity(new Intent(MemberWalletActivity.this, MemberWalletChargeListActivity.class));
                break;
            case R.id.member_wallet_cash:                            //跳转到提现记录
                startActivity(new Intent(MemberWalletActivity.this, MemberWalletCashListActivity.class));
                break;
            case R.id.member_wallet_discount:                    //跳转到优惠卷
                startActivity(new Intent(this, OrderMemberReleaseDiscountListActivity.class).putExtra("ocommission", "-10000"));
                break;
        }
    }
}
